﻿import 'dart:io';
import 'package:flutter/material.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'package:dotted_border/dotted_border.dart';
import 'text_record_logic.dart';

/// 文字记录页面
class TextRecordView extends GetView<TextRecordLogic> {
  const TextRecordView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFF282528),
      appBar: AppBar(
        backgroundColor: const Color(0xFF282528),
        elevation: 0,
        leading: GestureDetector(
          onTap: () => Get.back(),
          child: const Icon(
            Icons.arrow_back_ios,
            color: Colors.white,
            size: 20,
          ),
        ),
        title: const Text(
          '自定义添加食物',
          style: TextStyle(
            color: Colors.white,
            fontSize: 18,
            fontWeight: FontWeight.w500,
          ),
        ),
        centerTitle: true,
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(24.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [

            // 食物名称输入
            _buildInputField(
              label: '食物名称',
              placeholder: '如：水果沙拉，鸡蛋',
              controller: controller.foodNameController,
              isRequired: true,
            ),

           
             SizedBox(height: 386.h),

            // 保存按钮
            _buildSaveButton(),
            const SizedBox(height: 17),

            // 提示文字
            const Text(
              '注：请正确填写食物名称和数量及单位，确保AI计算卡路里时的准确性',
              style: TextStyle(
                color: Color(0xFFF7F7F7),
                fontSize: 14,
                fontWeight: FontWeight.w300
              ),
            ),
          ],
        ),
      ),
    );
  }

  

  /// 构建输入框
  Widget _buildInputField({
    required String label,
    required String placeholder,
    required TextEditingController controller,
    bool isRequired = false,
    TextInputType? keyboardType,
  }) {
    final focusNode = FocusNode();

    return StatefulBuilder(
      builder: (context, setState) {
        focusNode.addListener(() {
          setState(() {});
        });

        return Container(
          width: double.infinity,
          height: 143.h,
          decoration: BoxDecoration(
            color: const Color(0xFF282528),
            borderRadius: BorderRadius.circular(28),
            border: focusNode.hasFocus
                ? Border.all(
                    color: const Color(0xFFF45757),
                    width: 0.6,
                  )
                : null,
          ),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 左侧标签
              Padding(
                padding: const EdgeInsets.only(left: 20,top: 12),
                child: Row(
                  children: [
                    Text(
                      label,
                      style: const TextStyle(
                        color: Color(0xffDEDEDE),
                        fontSize: 20,
                      ),
                    ),
                    if (isRequired)
                      const Text(
                        ' *',
                        style: TextStyle(
                          color: Colors.red,
                          fontSize: 16,
                        ),
                      ),
                  ],
                ),
              ),
              // 右侧输入框
              Expanded(
                child: TextField(
                  controller: controller,
                  focusNode: focusNode,
                  keyboardType: keyboardType,
                  textAlign: TextAlign.right,
                  style: const TextStyle(
                    color: Colors.white,
                    fontSize: 16,
                  ),
                  decoration: InputDecoration(
                    hintText: placeholder,
                    hintStyle:  TextStyle(
                      color: Color(0xFFFFFFFFFF).withOpacity(0.6),
                      fontSize: 16,
                    ),
                    border: InputBorder.none,
                    contentPadding: const EdgeInsets.only(
                      right: 20,
                      top: 16,
                      bottom: 16,
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  /// 构建保存按钮
  Widget _buildSaveButton() {
    return Obx(() => GestureDetector(
          onTap: controller.canSave.value ? controller.saveFoodRecord : null,
          child: Container(
            width: double.infinity,
            height: 56,
            decoration: BoxDecoration(
              color: controller.canSave.value
                  ? const Color(0xFFED5C2F)
                  : const Color(0xFF404040),
              borderRadius: BorderRadius.circular(28),
            ),
            child: const Center(
              child: Text(
                '保存',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 18,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ),
          ),
        ));
  }
}
